{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from bokeh.plotting import figure, show, output_notebook\n",
    "from bokeh.io import push_notebook\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def update_quincunx(n, balls, buckets):\n",
    "    x0, x1, y = balls.T\n",
    "\n",
    "    # update buckets\n",
    "    buckets[x1[y == 1] + n] += 1\n",
    "\n",
    "    # update y\n",
    "    y[:] -= 1\n",
    "    y[y <= 0] = n\n",
    "\n",
    "    # update x\n",
    "    x1[y == n] = 0\n",
    "    x0[:] = x1\n",
    "    x1[:] += np.random.choice([-1, 1], len(x1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def animate_quincunx(time, balls):\n",
    "    x0, x1, y = balls.T\n",
    "\n",
    "    # animate position\n",
    "    x = x0 * (1 - time) + x1 * time\n",
    "    y = y * (1 - time) + (y - 1) * time\n",
    "    \n",
    "    return x, y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# quincunx size\n",
    "N = 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# row ~ ball, column ~ (current_x, next_x, current_y)\n",
    "balls = np.zeros((N - 1, 3), dtype=int)\n",
    "balls[:, 2] = np.arange(N - 1, dtype=int) + N + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# bottom buckets\n",
    "buckets = np.zeros(2 * N + 1, dtype=float)\n",
    "buckets_x = np.arange(-N, N + 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <a href=\"http://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
       "        <span id=\"59778c97-9325-4df5-8f3a-cfb5d4f73578\">Loading BokehJS ...</span>\n",
       "    </div>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "(function(global) {\n",
       "  function now() {\n",
       "    return new Date();\n",
       "  }\n",
       "\n",
       "  var force = true;\n",
       "\n",
       "  if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
       "    window._bokeh_onload_callbacks = [];\n",
       "    window._bokeh_is_loading = undefined;\n",
       "  }\n",
       "\n",
       "\n",
       "  \n",
       "  if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "    window._bokeh_timeout = Date.now() + 5000;\n",
       "    window._bokeh_failed_load = false;\n",
       "  }\n",
       "\n",
       "  var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "     \"<div style='background-color: #fdd'>\\n\"+\n",
       "     \"<p>\\n\"+\n",
       "     \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "     \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "     \"</p>\\n\"+\n",
       "     \"<ul>\\n\"+\n",
       "     \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "     \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "     \"</ul>\\n\"+\n",
       "     \"<code>\\n\"+\n",
       "     \"from bokeh.resources import INLINE\\n\"+\n",
       "     \"output_notebook(resources=INLINE)\\n\"+\n",
       "     \"</code>\\n\"+\n",
       "     \"</div>\"}};\n",
       "\n",
       "  function display_loaded() {\n",
       "    if (window.Bokeh !== undefined) {\n",
       "      var el = document.getElementById(\"59778c97-9325-4df5-8f3a-cfb5d4f73578\");\n",
       "      el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
       "    } else if (Date.now() < window._bokeh_timeout) {\n",
       "      setTimeout(display_loaded, 100)\n",
       "    }\n",
       "  }\n",
       "\n",
       "  function run_callbacks() {\n",
       "    window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
       "    delete window._bokeh_onload_callbacks\n",
       "    console.info(\"Bokeh: all callbacks have finished\");\n",
       "  }\n",
       "\n",
       "  function load_libs(js_urls, callback) {\n",
       "    window._bokeh_onload_callbacks.push(callback);\n",
       "    if (window._bokeh_is_loading > 0) {\n",
       "      console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "      return null;\n",
       "    }\n",
       "    if (js_urls == null || js_urls.length === 0) {\n",
       "      run_callbacks();\n",
       "      return null;\n",
       "    }\n",
       "    console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "    window._bokeh_is_loading = js_urls.length;\n",
       "    for (var i = 0; i < js_urls.length; i++) {\n",
       "      var url = js_urls[i];\n",
       "      var s = document.createElement('script');\n",
       "      s.src = url;\n",
       "      s.async = false;\n",
       "      s.onreadystatechange = s.onload = function() {\n",
       "        window._bokeh_is_loading--;\n",
       "        if (window._bokeh_is_loading === 0) {\n",
       "          console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
       "          run_callbacks()\n",
       "        }\n",
       "      };\n",
       "      s.onerror = function() {\n",
       "        console.warn(\"failed to load library \" + url);\n",
       "      };\n",
       "      console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "      document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "    }\n",
       "  };var element = document.getElementById(\"59778c97-9325-4df5-8f3a-cfb5d4f73578\");\n",
       "  if (element == null) {\n",
       "    console.log(\"Bokeh: ERROR: autoload.js configured with elementid '59778c97-9325-4df5-8f3a-cfb5d4f73578' but no matching script tag was found. \")\n",
       "    return false;\n",
       "  }\n",
       "\n",
       "  var js_urls = [\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.js\", \"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.js\"];\n",
       "\n",
       "  var inline_js = [\n",
       "    function(Bokeh) {\n",
       "      Bokeh.set_log_level(\"info\");\n",
       "    },\n",
       "    \n",
       "    function(Bokeh) {\n",
       "      \n",
       "    },\n",
       "    \n",
       "    function(Bokeh) {\n",
       "      \n",
       "      document.getElementById(\"59778c97-9325-4df5-8f3a-cfb5d4f73578\").textContent = \"BokehJS is loading...\";\n",
       "    },\n",
       "    function(Bokeh) {\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.5.min.css\");\n",
       "      console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n",
       "      Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.5.min.css\");\n",
       "    }\n",
       "  ];\n",
       "\n",
       "  function run_inline_js() {\n",
       "    \n",
       "    if ((window.Bokeh !== undefined) || (force === true)) {\n",
       "      for (var i = 0; i < inline_js.length; i++) {\n",
       "        inline_js[i](window.Bokeh);\n",
       "      }if (force === true) {\n",
       "        display_loaded();\n",
       "      }} else if (Date.now() < window._bokeh_timeout) {\n",
       "      setTimeout(run_inline_js, 100);\n",
       "    } else if (!window._bokeh_failed_load) {\n",
       "      console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "      window._bokeh_failed_load = true;\n",
       "    } else if (force !== true) {\n",
       "      var cell = $(document.getElementById(\"59778c97-9325-4df5-8f3a-cfb5d4f73578\")).parents('.cell').data().cell;\n",
       "      cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "    }\n",
       "\n",
       "  }\n",
       "\n",
       "  if (window._bokeh_is_loading === 0) {\n",
       "    console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "    run_inline_js();\n",
       "  } else {\n",
       "    load_libs(js_urls, function() {\n",
       "      console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "      run_inline_js();\n",
       "    });\n",
       "  }\n",
       "}(this));"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "\n",
       "    <div class=\"bk-root\">\n",
       "        <div class=\"bk-plotdiv\" id=\"1a574e4b-9e98-4fbd-b5ac-b69df07a26b8\"></div>\n",
       "    </div>\n",
       "<script type=\"text/javascript\">\n",
       "  \n",
       "  (function(global) {\n",
       "    function now() {\n",
       "      return new Date();\n",
       "    }\n",
       "  \n",
       "    var force = false;\n",
       "  \n",
       "    if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force === true) {\n",
       "      window._bokeh_onload_callbacks = [];\n",
       "      window._bokeh_is_loading = undefined;\n",
       "    }\n",
       "  \n",
       "  \n",
       "    \n",
       "    if (typeof (window._bokeh_timeout) === \"undefined\" || force === true) {\n",
       "      window._bokeh_timeout = Date.now() + 0;\n",
       "      window._bokeh_failed_load = false;\n",
       "    }\n",
       "  \n",
       "    var NB_LOAD_WARNING = {'data': {'text/html':\n",
       "       \"<div style='background-color: #fdd'>\\n\"+\n",
       "       \"<p>\\n\"+\n",
       "       \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n",
       "       \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n",
       "       \"</p>\\n\"+\n",
       "       \"<ul>\\n\"+\n",
       "       \"<li>re-rerun `output_notebook()` to attempt to load from CDN again, or</li>\\n\"+\n",
       "       \"<li>use INLINE resources instead, as so:</li>\\n\"+\n",
       "       \"</ul>\\n\"+\n",
       "       \"<code>\\n\"+\n",
       "       \"from bokeh.resources import INLINE\\n\"+\n",
       "       \"output_notebook(resources=INLINE)\\n\"+\n",
       "       \"</code>\\n\"+\n",
       "       \"</div>\"}};\n",
       "  \n",
       "    function display_loaded() {\n",
       "      if (window.Bokeh !== undefined) {\n",
       "        var el = document.getElementById(\"1a574e4b-9e98-4fbd-b5ac-b69df07a26b8\");\n",
       "        el.textContent = \"BokehJS \" + Bokeh.version + \" successfully loaded.\";\n",
       "      } else if (Date.now() < window._bokeh_timeout) {\n",
       "        setTimeout(display_loaded, 100)\n",
       "      }\n",
       "    }if ((window.Jupyter !== undefined) && Jupyter.notebook.kernel) {\n",
       "      comm_manager = Jupyter.notebook.kernel.comm_manager\n",
       "      comm_manager.register_target(\"e38d5958-093b-48f7-9abe-1e0a16e78d86\", function () {});\n",
       "    }\n",
       "  \n",
       "    function run_callbacks() {\n",
       "      window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
       "      delete window._bokeh_onload_callbacks\n",
       "      console.info(\"Bokeh: all callbacks have finished\");\n",
       "    }\n",
       "  \n",
       "    function load_libs(js_urls, callback) {\n",
       "      window._bokeh_onload_callbacks.push(callback);\n",
       "      if (window._bokeh_is_loading > 0) {\n",
       "        console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
       "        return null;\n",
       "      }\n",
       "      if (js_urls == null || js_urls.length === 0) {\n",
       "        run_callbacks();\n",
       "        return null;\n",
       "      }\n",
       "      console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
       "      window._bokeh_is_loading = js_urls.length;\n",
       "      for (var i = 0; i < js_urls.length; i++) {\n",
       "        var url = js_urls[i];\n",
       "        var s = document.createElement('script');\n",
       "        s.src = url;\n",
       "        s.async = false;\n",
       "        s.onreadystatechange = s.onload = function() {\n",
       "          window._bokeh_is_loading--;\n",
       "          if (window._bokeh_is_loading === 0) {\n",
       "            console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
       "            run_callbacks()\n",
       "          }\n",
       "        };\n",
       "        s.onerror = function() {\n",
       "          console.warn(\"failed to load library \" + url);\n",
       "        };\n",
       "        console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
       "        document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
       "      }\n",
       "    };var element = document.getElementById(\"1a574e4b-9e98-4fbd-b5ac-b69df07a26b8\");\n",
       "    if (element == null) {\n",
       "      console.log(\"Bokeh: ERROR: autoload.js configured with elementid '1a574e4b-9e98-4fbd-b5ac-b69df07a26b8' but no matching script tag was found. \")\n",
       "      return false;\n",
       "    }\n",
       "  \n",
       "    var js_urls = [];\n",
       "  \n",
       "    var inline_js = [\n",
       "      function(Bokeh) {\n",
       "        (function() {\n",
       "          var fn = function() {\n",
       "            var docs_json = {\"6be68d10-3acd-4652-9a67-0f03266b83a0\":{\"roots\":{\"references\":[{\"attributes\":{},\"id\":\"3c1e67f7-45d2-462e-8b60-15eec29f7db2\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.4},\"fill_color\":{\"value\":\"red\"},\"line_color\":{\"value\":\"red\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"c6709d73-7a50-4585-830e-ea0891df5338\",\"type\":\"VBar\"},{\"attributes\":{\"data_source\":{\"id\":\"0f51c37f-a40c-47de-9ca5-9e67f5ef9bfe\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"c6709d73-7a50-4585-830e-ea0891df5338\",\"type\":\"VBar\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"8100a04b-b78b-4d05-bedb-04bc4ef56197\",\"type\":\"VBar\"},\"selection_glyph\":null},\"id\":\"145d2028-6013-424b-af20-8f01298cde02\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"07d9d9b4-2e70-4202-a54f-2e980c2be003\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"fill_color\":{\"value\":\"#1f77b4\"},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"6510f2d2-3617-49d2-a1b4-6b581fb8e28e\",\"type\":\"Circle\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"top\":{\"field\":\"top\"},\"width\":{\"value\":0.8},\"x\":{\"field\":\"x\"}},\"id\":\"8100a04b-b78b-4d05-bedb-04bc4ef56197\",\"type\":\"VBar\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"38982f12-b28a-4821-987c-866dc2387559\",\"type\":\"Circle\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"y\"],\"data\":{\"x\":[2,-3,4,3,0,1,-2,-1,0,-1,6,-5,-6,-3,4,5,-10,-7,6,3,2,7,-2,3,-8,-3,2,-1,-2],\"y\":[20,21,22,23,24,25,26,27,28,29,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]}},\"id\":\"abcf0a4a-3b9e-4620-bf9b-e49773cc160a\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data_source\":{\"id\":\"abcf0a4a-3b9e-4620-bf9b-e49773cc160a\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"6510f2d2-3617-49d2-a1b4-6b581fb8e28e\",\"type\":\"Circle\"},\"hover_glyph\":null,\"muted_glyph\":null,\"nonselection_glyph\":{\"id\":\"38982f12-b28a-4821-987c-866dc2387559\",\"type\":\"Circle\"},\"selection_glyph\":null},\"id\":\"a99f361d-6d6c-4395-a2dd-84f40d3003a8\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"callback\":null,\"end\":30,\"start\":-30},\"id\":\"b7d76723-5292-422b-8d18-bf8db80a2f9d\",\"type\":\"Range1d\"},{\"attributes\":{\"below\":[{\"id\":\"1a3f3260-8488-42b0-b5d4-f76d9ec624aa\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"60b043bd-e19d-4d69-96d4-a812332bc591\",\"type\":\"LinearAxis\"}],\"plot_height\":420,\"plot_width\":640,\"renderers\":[{\"id\":\"1a3f3260-8488-42b0-b5d4-f76d9ec624aa\",\"type\":\"LinearAxis\"},{\"id\":\"d12df254-7687-4acc-bb01-ff63149544d6\",\"type\":\"Grid\"},{\"id\":\"60b043bd-e19d-4d69-96d4-a812332bc591\",\"type\":\"LinearAxis\"},{\"id\":\"3371c4ce-4911-470e-8711-5653257159d1\",\"type\":\"Grid\"},{\"id\":\"145d2028-6013-424b-af20-8f01298cde02\",\"type\":\"GlyphRenderer\"},{\"id\":\"a99f361d-6d6c-4395-a2dd-84f40d3003a8\",\"type\":\"GlyphRenderer\"}],\"title\":{\"id\":\"cd2c5ce8-0e52-47bb-89aa-0c698884f8e9\",\"type\":\"Title\"},\"tool_events\":{\"id\":\"d48e54ce-5bd3-4a23-9e23-c0d9092fbb27\",\"type\":\"ToolEvents\"},\"toolbar\":{\"id\":\"59423cde-e4ae-42f8-bb13-f8e15942a429\",\"type\":\"Toolbar\"},\"x_range\":{\"id\":\"b7d76723-5292-422b-8d18-bf8db80a2f9d\",\"type\":\"Range1d\"},\"y_range\":{\"id\":\"857c3906-20df-4150-bcd4-7189399cd685\",\"type\":\"Range1d\"}},\"id\":\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{},\"id\":\"d48e54ce-5bd3-4a23-9e23-c0d9092fbb27\",\"type\":\"ToolEvents\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x\",\"top\"],\"data\":{\"top\":{\"__ndarray__\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADwPwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAQQAAAAAAAAAAAAAAAAAAAJEAAAAAAAAAAAAAAAAAAAAhAAAAAAAAAAAAAAAAAAAAqQAAAAAAAAAAAAAAAAAAAIkAAAAAAAAAAAAAAAAAAABRAAAAAAAAAAAAAAAAAAAAiQAAAAAAAAAAAAAAAAAAAFEAAAAAAAAAAAAAAAAAAABBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=\",\"dtype\":\"float64\",\"shape\":[61]},\"x\":[-30,-29,-28,-27,-26,-25,-24,-23,-22,-21,-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]}},\"id\":\"0f51c37f-a40c-47de-9ca5-9e67f5ef9bfe\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\"},\"id\":\"59423cde-e4ae-42f8-bb13-f8e15942a429\",\"type\":\"Toolbar\"},{\"attributes\":{\"formatter\":{\"id\":\"07d9d9b4-2e70-4202-a54f-2e980c2be003\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"e774019f-c033-45f1-a7c4-fbfd3b33093c\",\"type\":\"BasicTicker\"}},\"id\":\"1a3f3260-8488-42b0-b5d4-f76d9ec624aa\",\"type\":\"LinearAxis\"},{\"attributes\":{\"plot\":null,\"text\":\"\"},\"id\":\"cd2c5ce8-0e52-47bb-89aa-0c698884f8e9\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"e774019f-c033-45f1-a7c4-fbfd3b33093c\",\"type\":\"BasicTicker\"},{\"attributes\":{\"formatter\":{\"id\":\"3c1e67f7-45d2-462e-8b60-15eec29f7db2\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"8fc3350c-dabc-43a4-87ed-92f13c6b8675\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"60b043bd-e19d-4d69-96d4-a812332bc591\",\"type\":\"LinearAxis\"},{\"attributes\":{\"plot\":{\"id\":\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"e774019f-c033-45f1-a7c4-fbfd3b33093c\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"d12df254-7687-4acc-bb01-ff63149544d6\",\"type\":\"Grid\"},{\"attributes\":{\"callback\":null,\"end\":30},\"id\":\"857c3906-20df-4150-bcd4-7189399cd685\",\"type\":\"Range1d\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\",\"subtype\":\"Figure\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"8fc3350c-dabc-43a4-87ed-92f13c6b8675\",\"type\":\"BasicTicker\"},\"visible\":false},\"id\":\"3371c4ce-4911-470e-8711-5653257159d1\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"8fc3350c-dabc-43a4-87ed-92f13c6b8675\",\"type\":\"BasicTicker\"}],\"root_ids\":[\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.5\"}};\n",
       "            var render_items = [{\"docid\":\"6be68d10-3acd-4652-9a67-0f03266b83a0\",\"elementid\":\"1a574e4b-9e98-4fbd-b5ac-b69df07a26b8\",\"modelid\":\"9fe75c1b-77e7-47a2-93fe-cbfa39fb6bff\",\"notebook_comms_target\":\"e38d5958-093b-48f7-9abe-1e0a16e78d86\"}];\n",
       "            \n",
       "            Bokeh.embed.embed_items(docs_json, render_items);\n",
       "          };\n",
       "          if (document.readyState != \"loading\") fn();\n",
       "          else document.addEventListener(\"DOMContentLoaded\", fn);\n",
       "        })();\n",
       "      },\n",
       "      function(Bokeh) {\n",
       "      }\n",
       "    ];\n",
       "  \n",
       "    function run_inline_js() {\n",
       "      \n",
       "      if ((window.Bokeh !== undefined) || (force === true)) {\n",
       "        for (var i = 0; i < inline_js.length; i++) {\n",
       "          inline_js[i](window.Bokeh);\n",
       "        }if (force === true) {\n",
       "          display_loaded();\n",
       "        }} else if (Date.now() < window._bokeh_timeout) {\n",
       "        setTimeout(run_inline_js, 100);\n",
       "      } else if (!window._bokeh_failed_load) {\n",
       "        console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n",
       "        window._bokeh_failed_load = true;\n",
       "      } else if (force !== true) {\n",
       "        var cell = $(document.getElementById(\"1a574e4b-9e98-4fbd-b5ac-b69df07a26b8\")).parents('.cell').data().cell;\n",
       "        cell.output_area.append_execute_result(NB_LOAD_WARNING)\n",
       "      }\n",
       "  \n",
       "    }\n",
       "  \n",
       "    if (window._bokeh_is_loading === 0) {\n",
       "      console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
       "      run_inline_js();\n",
       "    } else {\n",
       "      load_libs(js_urls, function() {\n",
       "        console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
       "        run_inline_js();\n",
       "      });\n",
       "    }\n",
       "  }(this));\n",
       "</script>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "output_notebook()\n",
    "\n",
    "# figure\n",
    "plot = figure(plot_width=640, plot_height=420, tools='',\n",
    "              x_range=(-N, N), y_range=(0, N))\n",
    "plot.grid.visible = False\n",
    "plot.yaxis.visible = False\n",
    "\n",
    "# initial plot\n",
    "x, y = animate_quincunx(1, balls)\n",
    "\n",
    "vbar = plot.vbar(x=buckets_x, width=.8, bottom=0, top=buckets,\n",
    "                 fill_alpha=.4, fill_color='red', line_color='red')\n",
    "scatter = plot.scatter(x, y)\n",
    "\n",
    "# show\n",
    "handle = show(plot, notebook_handle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(N * 5 * 30):\n",
    "    time = round(i / 5 % 1, 1)\n",
    "\n",
    "    # update bars\n",
    "    if time == 0:\n",
    "        update_quincunx(N, balls, buckets)\n",
    "        vbar.data_source.data['top'] = buckets * .2\n",
    "        \n",
    "    # update scatter\n",
    "    x, y = animate_quincunx(time, balls)\n",
    "    scatter.data_source.data['x'] = x\n",
    "    scatter.data_source.data['y'] = y\n",
    "\n",
    "    push_notebook()\n",
    "    sleep(.05)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
